package nio.three;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/*
   客户端接收消息的线程
 */
public class ClientAcceptThread extends Thread {
    private SocketChannel socketChannel;

    public ClientAcceptThread(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    @Override
    public void run() {
        try {
            while (true) {
                //创建缓冲区
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                int len = 0;
                while ((len = socketChannel.read(buffer)) != 0) {       //读取数据到缓冲区
                    //将数据输出
                    buffer.flip();
                    System.out.println(new String(buffer.array(), 0, len));
                    buffer.clear();
                }
            }
        } catch (Exception e) {
        }
    }
}
